package com.zzy.product.controller;

import com.zzy.product.entity.ProductAttrValueEntity;
import com.zzy.product.service.ProductAttrValueService;
import com.zzy.product.vo.AttrRespVo;
import com.zzy.product.vo.AttrVo;
import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.R;
import com.zzy.product.entity.AttrEntity;
import com.zzy.product.service.AttrService;
import jdk.jpackage.internal.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author kazu
 * @email kazu_zhang@163.com
 * @date 2023-02-01 21:39:21
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
	@Autowired
	private AttrService attrService;
	
	@Autowired
	private ProductAttrValueService valueService;
	
	/**
	 * /product/attr/base/listforspu/{spuId}
	 */
	@GetMapping("/base/listforspu/{spuId}")
	public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
		List<ProductAttrValueEntity> data = valueService.baseAttrListForSpu(spuId);
		return R.ok().put("data", data);
	}
	
	/**
	 * /api/product/attr/base/list
	 */
	@GetMapping("{attrType}/list/{catelogId}")
	public R baseAttrList(@RequestParam Map<String, Object> params,
						  @PathVariable("catelogId") Long catelogId,
						  @PathVariable String attrType) {
		PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
		
		return R.ok().put("page", page);
	}
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = attrService.queryPage(params);
		
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrId}")
	public R info(@PathVariable("attrId") Long attrId) {
		AttrRespVo respVo = attrService.getAttrInfo(attrId);
		
		return R.ok().put("attr", respVo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody AttrVo attr) {
		attrService.saveAttr(attr);
		
		return R.ok();
	}
	
	/**
	 * /product/attr/update/{spuId}
	 * @return
	 */
	@PostMapping("/update/{spuId}")
	public R updateAttr(@PathVariable("spuId") Long spuId,
						@RequestBody List<ProductAttrValueEntity> entities) {
		valueService.updateSpuAttr(spuId, entities);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody AttrVo attr) {
		attrService.updateAttr(attr);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] attrIds) {
		attrService.removeByIds(Arrays.asList(attrIds));
		
		return R.ok();
	}
	
}
